
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Account extends CI_Controller {

    public function index() {
        if (update_user() == 3) {
            $Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
            $this->load->view('user_group3/account_management/account_management_main', $Array_Data);
        } else {
            redirect(site_url() . '/welcome/not_authorized');
        }
    }

    public function fill_in() {
        if (update_user(false, true) === 3) {
            $Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
            $this->load->view('user_group3/fill_information', $Array_Data);
        } else {
            $this->load->view('index.html');
        }
    }

    public function update_user_information() {
        $this->load->helper('string_addition');
        $UserInfo = $this->session->userdata('UserInfo');
        $RelUserEmail = $UserInfo['UserEmail'];
        $Name = $_POST['UserName'];
        $Sex = $_POST['Sex'];
        $Age = $_POST['Age'];
        $Address = "###";

        $Qualifications = strip_string($_POST['Qualification']);
        $Salary = $_POST['Salary'];
        $Currency = $_POST['currency'];

        $WorkingPlace = $_POST['working-place'];
        $ProfileLinks = strip_string($_POST['profile-link']);
        $Desc = $_POST['description'];
        $MarriedStatus = $_POST['MarriedStatus'];
        $data = array(
            'RelUserEmail' => $RelUserEmail,
            'UserFullname' => $Name,
            'Age' => $Age,
            'Sex' => $Sex,
            'MarriedStatus' => $MarriedStatus,
            'Address' => $Address,
            'Qualification' => $Qualifications,
            'Salary' => $Salary,
            'Currency' => $Currency,
            'WorkingPlace' => $WorkingPlace,
            'ProfileLinks' => $ProfileLinks,
            'Describtion' => $Desc,
            'AvatarLink' => $_POST['AvatarLink']
        );
        $this->db->insert(MYSQL_TABLE_PREFLIX . TABLE_USER3_ACCOUNTS_INFORMATION, $data);
        redirect(site_url());
    }

    public function upload_avatar() {
        $rand = uniqid(rand());
        $ext = pathinfo($_FILES['avatar']['name'], PATHINFO_EXTENSION);
        $UserInfo = $this->session->userdata('UserInfo');
        $file_name = md5($UserInfo['UserEmail']) . '.' . $ext;
        $target_path = image_url(true) . $file_name;

        if (@move_uploaded_file($_FILES['avatar']['tmp_name'], $target_path)) {
            $result = $file_name;
        }
        echo '<script language="javascript" type="text/javascript">window.top.window.StopUpload("' . $result . '");</script>';
    }

    public function edit_user_account() {
        $UserInfo = $this->session->userdata('UserInfo');
        $this->load->helper('string_addition');
        $UserFullname = $_POST['UserFullname'];
        $UserOldPass = $_POST['OldPassword'];
        $Age = $_POST['Age'];
        $Sex = $_POST['Sex'];
        $MarriedStatus = $_POST['MarriedStatus'];
        $AvatarLink = $_POST['AvatarLink'];
        $changePass = false;
        if (!empty($UserOldPass)) {
            $UserPassword = $_POST['NewPassword'];
            $changePass = true;
        }
        $data = array(
            "UserFullname" => $UserFullname,
            "Age" => $Age,
            "Sex" => $Sex,
            "MarriedStatus" => $MarriedStatus,
            "AvatarLink" => $AvatarLink
        );
        $this->db->where('RelUserEmail', $UserInfo['UserEmail']);
        $this->db->update(MYSQL_TABLE_PREFLIX . TABLE_USER3_ACCOUNTS_INFORMATION, $data);

        if ($changePass) {
            $data = array(
                'UserPassword' => md5($UserPassword)
            );
            $this->db->where('UserEmail', $UserInfo['UserEmail']);
            $this->db->update(MYSQL_TABLE_PREFLIX . TABLE_USER_ACCOUNTS, $data);
            $UserInfo['UserPassword'] = md5($UserPassword);
            $this->session->set_userdata('UserInfo', $UserInfo);
        }

        redirect(site_url() . '/user_group3/account');
    }

    public function edit_user_contact() {
        $this->load->helper('string_addition');
        $UserInfo = $this->session->userdata('UserInfo');

        $Address = strip_string($_POST['Address']);
        $Phones = strip_string($_POST['Phones']);
        $ProfileLinks = strip_string($_POST['ProfileLinks']);
        $data = array(
            "Address" => $Address,
            "Phone" => $Phones,
            "ProfileLinks" => $ProfileLinks,
        );
        $this->db->where('RelUserEmail', $UserInfo['UserEmail']);
        $this->db->update(MYSQL_TABLE_PREFLIX . TABLE_USER3_ACCOUNTS_INFORMATION, $data);
        redirect(site_url() . '/user_group3/account?section=user-contact');
    }

    public function edit_user_qualification() {
        $UserInfo = $this->session->userdata('UserInfo');
        $this->load->helper('string_addition');
        $ID = $_POST['currentid'];
        $QualificationName = strip_string($_POST['QualificationName']);
        $Qualification = $_POST['Qualification'];
        $QualificationLevel = $_POST['QualificationLevel'];
        $ConfirmLink = $_POST['ConfirmLink'];
        $Year = $_POST['Year'];
        //Update qualification first
        $data = array('Qualification' => $Qualification);
        $this->db->where('RelUserEmail', $UserInfo['UserEmail']);
        $this->db->update(MYSQL_TABLE_PREFLIX . TABLE_USER3_ACCOUNTS_INFORMATION, $data);
        //Update addional qualification
        $data = array(
            'RelUserEmail' => $UserInfo['UserEmail'],
            'QualificationName' => $QualificationName,
            'QualificationLevel' => $QualificationLevel,
            'ConfirmLink' => $ConfirmLink,
            'Year' => $Year
        );

        if ($ID != -1) {
            $this->db->where('QualificationID', $ID);
            $this->db->update(MYSQL_TABLE_PREFLIX . TABLE_USER3_ADDITIONAL_QUALIFICATION, $data);
        } else {
            $this->db->insert(MYSQL_TABLE_PREFLIX . TABLE_USER3_ADDITIONAL_QUALIFICATION, $data);
        }

        redirect(site_url() . '/user_group3/account?section=edit-qualification');
    }

    public function edit_user_workedplace() {
        $UserInfo = $this->session->userdata('UserInfo');
        $ID = $_POST['currentid'];
        $WorkingPlace = $_POST['WorkingPlace'];
        $this->db->where('RelUserEmail', $UserInfo['UserEmail']);
        $this->db->update(MYSQL_TABLE_PREFLIX . TABLE_USER3_ACCOUNTS_INFORMATION, array('WorkingPlace' => $WorkingPlace));
        if ($ID != -2) {
            $CompanyName = $_POST['CompanyName'];
            $CompanyLink = $_POST['CompanyLink'];
            $Year = $_POST["Year"];
            $Describtion = $_POST['Describtion'];

            $data = array(
                'RelUserEmail' => $UserInfo['UserEmail'],
                'CompanyName' => $CompanyName,
                'CompanyLink' => $CompanyLink,
                'Year' => $Year,
                'Describtion' => $Describtion
            );

            if ($ID != -1) {
                $this->db->where('WorkedPlaceID', $ID);
                $this->db->update(MYSQL_TABLE_PREFLIX . TABLE_USER3_WORKEDPLACES, $data);
            } else {
                $this->db->insert(MYSQL_TABLE_PREFLIX . TABLE_USER3_WORKEDPLACES, $data);
            }
        }
        redirect(site_url() . '/user_group3/account?section=edit-experience');
    }

    public function edit_user_hobbies() {
        $CI = & get_instance();
        $CI->load->model('Users3_model', 'Us3');

        $UserInfo = $this->session->userdata('UserInfo');
        $this->load->helper('string_addition');
        $Hobbies = strip_string($_POST['hobbies'], '##');
        $data = array(
            "Hobbies" => $Hobbies,
        );
        $CI->Us3->Update(array('RelUserEmail' => $UserInfo['UserEmail']), $data);
        redirect(site_url() . '/user_group3/account?section=edit-hobbies');
    }
    
    public function edit_user_activities(){
        $UserInfo = $this->session->userdata('UserInfo');
        if(($ID = $_POST['currentid']) == -2)
            redirect(site_url() . '/user_group3/account?section=edit-activities');
        $CI =& get_instance();      
        $CI->load->model('Users3_Activities_model', 'Us3Act');
        
        $CI->Us3Act->Insert(array(
            'RelUserID'         =>  $UserInfo['UserGroup3ID'],
            'ActTitle'          =>  $_POST['ActTitle'],
            'ActYear'           =>  $_POST['ActYear'],
            'ActDescribtion'    =>  $_POST['ActDescribtion']
        ));
        
        redirect(site_url() . '/user_group3/account?section=edit-activities');
    }

    public function delete_qualification() {
        $UserInfo = $this->session->userdata('UserInfo');
        $this->db->delete(MYSQL_TABLE_PREFLIX . TABLE_USER3_ADDITIONAL_QUALIFICATION, array(
            'QualificationID' => $_POST['ID'],
            'RelUserEmail' => $UserInfo['UserEmail']
        ));

        echo "1";
    }

    public function delete_workedplace() {
        $UserInfo = $this->session->userdata('UserInfo');
        $this->db->delete(MYSQL_TABLE_PREFLIX . TABLE_USER3_WORKEDPLACES, array(
            'WorkedPlaceID' => $_POST['ID'],
            'RelUserEmail'  => $UserInfo['UserEmail']
        ));

        echo "1";
    }

    public function delete_activity() {
        $UserInfo = $this->session->userdata('UserInfo');
        $CI =& get_instance();
        
        $CI->load->model('Users3_Activities_model', 'Us3Act');
        $CI->Us3Act->Delete(array(
            'US3ActID'  =>  $_POST['ID'],
            'RelUserID' =>  $UserInfo['UserGroup3ID']
        ));
        echo "1";
    }
    
    public function show($ID) {
        $CI =& get_instance();
        $Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
        $CI->load->model('Users3_model', 'Users3');
        $CI->load->model('Users3_Qualification_model', 'Users3Qual');
        $CI->load->model('Users3_WorkedPlaces_model', 'Exp');
        $CI->load->model('Users3_Activities_model', 'Us3Act');
        
        $Array_Data['DesInfo'] = $CI->Users3->GetInformation(array('UserGroup3ID' => $ID));
        $Array_Data['DesInfo'] = $Array_Data['DesInfo'][0];
        $Array_Data['DesInfo']['Qualifications'] = $CI->Users3Qual->Get(array(
            'RelUserEmail' => $Array_Data['DesInfo']['RelUserEmail']
        ));
        $Array_Data['DesInfo']['WorkedPlaces']  = $CI->Exp->Get_Advanced("*", 
                array('RelUserEmail' => $Array_Data['DesInfo']['RelUserEmail']), 
                array('Year')
        );
        
        $Array_Data['DesInfo']['Activities']    = $CI->Us3Act->Get_Advanced("*",
                array('RelUserID'   =>  $Array_Data['DesInfo']['UserGroup3ID']),
                "ActYear ASC"
                );
        $this->load->view('user_group3/show_view', $Array_Data);
    }

    public function exportCV($Format = 'pdf') {
        
    }

}
